import {
  IModalData,
  IOpenViewUtil,
  IPopoverOptions,
  Placement,
} from '@ibiz-template/runtime';
import {
  generateRoutePath,
  generateRoutePathByModal,
  getDrawerPlacement,
  openViewDrawer,
  openViewModal,
  openViewPopover,
  routerCallback,
} from '@ibiz-template/vue3-util';
import { Router } from 'vue-router';
import { FloatingUIConfig } from '../app-popover/app-popover-component';

/**
 * 打开视图方式工具类
 *
 * @description 此实现类挂载在 ibiz.openViewUtil
 * @author chitanda
 * @date 2022-08-16 20:08:54
 * @export
 * @class OpenViewUtil
 * @implements {IOpenViewUtil}
 */
export class OpenViewUtil implements IOpenViewUtil {
  // eslint-disable-next-line no-useless-constructor, no-empty-function
  constructor(protected router: Router) {}

  push(path: string): Promise<IModalData> {
    return routerCallback.open(this.router, path);
  }

  async root(
    appViewId: string,
    context: IContext,
    params?: IParams,
    modalOptions?: IData,
  ): Promise<IModalData> {
    const appView = await ibiz.hub.config.view.get(appViewId!);
    const { path } = await generateRoutePath(
      appView,
      this.router.currentRoute.value,
      context,
      params,
    );
    if (modalOptions && modalOptions.replace === true) {
      this.router.replace({ path });
    } else {
      this.router.push({ path });
    }
    return { ok: true };
  }

  async rootByModal(
    appViewId: string,
    context: IContext,
    params?: IParams,
  ): Promise<IModalData> {
    const appView = await ibiz.hub.config.view.get(appViewId!);
    const { path } = await generateRoutePathByModal(
      appView,
      this.router.currentRoute.value,
      context,
      params,
    );
    this.router.push({ path });
    return { ok: true };
  }

  /**
   * 模态打开视图
   *
   * @author lxm
   * @date 2022-09-12 01:09:06
   * @param {string} appViewId
   * @param {(IContext)} [context]
   * @param {(IParams)} [params]
   * @returns {*}  {Promise<IModalData>}
   */
  async modal(
    appViewId: string,
    context: IContext,
    params?: IParams,
  ): Promise<IModalData> {
    const appView = await ibiz.hub.config.view.get(appViewId!);
    // 设置默认的modal参数
    const opts = {
      width: appView.width || '80%',
      height: appView.height || '80%',
      footerHide: true,
      ...appView.modalOption,
    };
    return openViewModal(
      {
        context,
        params,
        viewId: appView.id,
      },
      opts,
    );
  }

  async popover(
    appViewId: string,
    event: MouseEvent,
    context: IContext,
    params?: IParams,
    options: IPopoverOptions<FloatingUIConfig> = {},
  ): Promise<IModalData> {
    const appView = await ibiz.hub.config.view.get(appViewId!);
    // 设置默认的modal参数
    const opts = {
      width: appView.width,
      height: appView.height,
      autoClose: true,
      placement: 'bottom' as Placement,
      ...options,
      ...appView.modalOption,
    };
    return openViewPopover(
      event,
      {
        context,
        params,
        viewId: appView.id,
      },
      opts,
    );
  }

  /**
   * 抽屉打开视图
   *
   * @author lxm
   * @date 2022-09-15 15:09:50
   * @param {string} appViewId
   * @param {(IContext)} [context]
   * @param {(IParams)} [params]
   * @returns {*}  {Promise<IModalData>}
   */
  async drawer(
    appViewId: string,
    context: IContext,
    params?: IParams,
  ): Promise<IModalData> {
    const appView = await ibiz.hub.config.view.get(appViewId!);
    const placement = getDrawerPlacement(appView.openMode!);

    // 设置默认的modal参数
    const opts = {
      width: appView.width,
      height: appView.height,
      placement,
      ...appView.modalOption,
    };

    return openViewDrawer(
      {
        context,
        params,
        viewId: appView.id,
      },
      opts,
    );
  }

  async custom(
    appViewId: string,
    context: IContext,
    params?: IParams,
  ): Promise<IModalData> {
    const appView = await ibiz.hub.config.view.get(appViewId!);
    ibiz.log.warn('openUserCustom', appView, context, params);
    throw new Error();
  }
}
